package com.gitee.huanminabc.jcommon.encryption;

import com.gitee.huanminabc.jcommon.file.FileReadUtil;
import com.gitee.huanminabc.jcommon.file.FileWriterUtil;
import com.gitee.huanminabc.jcommon.str.StringUtil;
import com.gitee.huanminabc.jcommon.system.AppStop;
import lombok.SneakyThrows;

import java.io.File;
import java.io.IOException;

/**
 * 外部通过调用 Class.forName("com.gitee.huanminabc.common.encryption.XkzVerify");来自动触发许可证验证
 */
public class XkzVerify {
    private static String KEY = "1234567891234567";
    private static String IV = "39400A13BD6BE6C8";
    private static String MESSAGE = "6K645Y+v6K+B5paH5Lu25LiN5a2Y5Zyo77yM6K+35Zyo55So5oi355uu5b2V5LiL5re75YqgaHVhbm1pbi1saWNlbnNlLnR4dOiuuOWPr+ivgeaWh+S7tuOAgg==";
    private static String MESSAGE_1 = "6K645Y+v6K+B5YaF5a656ZSZ6K+v77yM6K+35re75Yqg5q2j56Gu55qE6K645Y+v6K+B44CC"; //许可证内容错误，请添加正确的许可证
    private static String MESSAGE_2 = "6K645Y+v6K+B5paH5Lu25YaF5a655piv56m655qE77yM6K+35re75Yqg5q2j56Gu55qE6K645Y+v6K+B44CC";//许可证文件内容是空的，请添加正确的许可证。
    private static String MESSAGE_3 = "6K645Y+v6K+B6L+H5pyf77yM6K+35pu05o2i5paw55qE6K645Y+v6K+B44CC";//许可证过期，请更换新的许可证。
    private static int LIMIT_TIME = 60 * 10 * 1000;
    private static String CIPHER_TEXT = "6Zi/5piv5Y+R6aG65Liw5Zm25rKZ5Y+R5LiK5Y+R55Sf5aSp5rCU5oyW5rOV5rua5ZCn5L2g5ZCI5rOV5Liq";
    private static String FILE_NAME = "aHVhbm1pbi1saWNlbnNlLnR4dA==";
    private static String USER_HOME = "dXNlci5ob21l";

    static {
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(LIMIT_TIME);
                    verify();
                }  catch (InterruptedException ignored) {
                }

            }
        });
        thread.setDaemon(true);
        thread.start();

    }

    private static String readVerify() throws IOException {
        String userDir = System.getProperty(Base64Util.decode(USER_HOME));
        String filePath = userDir + File.separator + Base64Util.decode(FILE_NAME);
        File file = new File(filePath);
        if (!file.exists()) {
            System.err.println(userDir+ File.separator +Base64Util.decode(FILE_NAME)+Base64Util.decode(MESSAGE));
            Runtime.getRuntime().halt(1);
        }
        String license = FileReadUtil.readAllStr(file);
        if (StringUtil.isEmpty(license)) {
            System.err.println(userDir+ File.separator +Base64Util.decode(FILE_NAME)+Base64Util.decode(MESSAGE_2));
            AppStop.stop();
        }
        return license;
    }

    //验证许可证
    @SneakyThrows
    private static void verify()  {
        String userDir = System.getProperty(Base64Util.decode(USER_HOME));
        String license = readVerify();
        String decrypt = AESCBCUtil.decrypt(license, KEY, IV);
        //分割### 之前的是密文,之后的是有效期
        String[] split = decrypt.split("###");
        if (split.length != 2) {
            System.err.println(userDir+ File.separator +Base64Util.decode(FILE_NAME)+Base64Util.decode(MESSAGE_1));
            AppStop.stop();
        }
        long deadline = Long.parseLong(split[1]);
        if (System.currentTimeMillis() > deadline) {
            System.err.println(userDir+ File.separator +Base64Util.decode(FILE_NAME)+Base64Util.decode(MESSAGE_3));
            AppStop.stop();
        }
        if (!CIPHER_TEXT.equals(split[0])) {
            System.err.println(userDir+ File.separator +Base64Util.decode(FILE_NAME)+Base64Util.decode(MESSAGE_1));
            AppStop.stop();
        }
    }


    //随机生成一个hu-license.txt文件,里面存放的是ase密文
    //deadlineDay 有效期天数,以当前时间为起点
    private static void createLicense(int deadlineDay) throws IOException {
        String userDir = System.getProperty(Base64Util.decode(USER_HOME));
        String filePath = userDir + File.separator + Base64Util.decode(FILE_NAME);
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        //当前时间+有效期天数
        long deadline = System.currentTimeMillis() + (long) deadlineDay * 24 * 60 * 60 * 1000;
        String content = CIPHER_TEXT+"###" + deadline;
        String encrypt = AESCBCUtil.encrypt(content, KEY, IV);
        FileWriterUtil.writerStrToFile(encrypt,file);
    }

    public static void main(String[] args) throws IOException {
        createLicense(360);
//        verify();
    }


}
